<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <!-- 插值语法 -->
      <h3>{{fullName}}</h3>

      <!-- 指令语法 -->
      单向数据绑定:<input type="text" :value="firstName" /> <br />
      双向绑定:<input type="text" v-model="lastName" /><br />

      <!-- 条件渲染 -->
      <div v-show="true">{{name}},{{a}}</div>
      <div v-if="a === 1 ">c++</div>
      <div v-else-if="a === 2">java</div>
      <div v-else-if="a === 3">python</div>
      <div v-else>哈哈</div>

      <template v-if="a===3">
        <h2>hello</h2>
        <h2>hello</h2>
        <h2>hello</h2>
      </template>
      <button @click="add()">点我a+1</button>

      <!-- 遍历数组 -->
      <h2>人员列表</h2>
      <ul>
        <!-- index 从0开始一次递增，key最好写上 -->
        <li v-for=" (person,index) in persons" :key="index">
          {{person.name}} - {{person.age}} -{{index}}
        </li>
      </ul>
      <!-- 遍历对象 -->
      <h2>汽车信息</h2>
      <ul>
        <li v-for="(key,val) in car" :key="key">{{key}} - {{val}}</li>
      </ul>
      <!-- 遍历字符串 -->
      <h2>遍历字符串</h2>
      <ul>
        <li v-for="(val,index) in str" :key="index">{{index}} - {{val}}</li>
      </ul>
    </div>
    <script type="text/javascript">
      Vue.config.productionTip = false
      const vm = new Vue({
        el: '#root',
        data: {
          a: 0,
          firstName: '张',
          lastName: '三',
          persons: [
            { id: '01', name: '张三', age: 18 },
            { id: '02', name: '李四', age: 28 },
            { id: '03', name: '王五', age: 38 },
          ],
          car: {
            name: '奥迪A8',
            price: 700000,
            color: 'red',
          },
          str: 'hello',
        },
        methods: {
          add() {
            this.a++
          },
        },
        computed: {
          // 计算属性(简写方式)
          //通过firstName和lastName计算出fullName的值
          fullName() {
            return this.firstName + '-' + this.lastName
          },
        },
        watch: {
          // 侦听属性, 当某个属性值发生改变时,调用handler方法
          //newValue  改变后的值
          //oldValue  改变前的值
          firstName(newValue, oldValue) {
            deep: true //深度侦听
            console.log('fistName的值改变成了' + newValue)
          },
        },
      })
    </script>
  </body>
</html>
